import test from 'ava';
import R from 'ramda';

import { previous,
         next,
         tabReducer } from '../../src/javascript/modules/tab/reducer.js';


const streamers = [
  {
    id: 1,
    name: 'callmepieman',
    twitch: 'callmepieman',
    kageshi: 'pieman',
  }, {
    id: 2,
    name: 'james',
    twitch: 'james',
    kageshi: 'james',
  }, {
    id: 3,
    name: 'john',
    twitch: 'john',
    kageshi: 'john',
  },
];

function initState() {
  return Object.assign({ },
                       { selected: R.head(streamers) },
                       { previous: R.last(streamers) },
                       { next: R.head(R.tail(streamers)) },
                       { streamers });
}

function backward(state) {
  return Object.assign({},
                       state,
                       previous(state));
}
function forward(state) {
  return Object.assign({},
                       state,
                       next(state));
}

test('next', t => {
  const state = initState();
  const nextState = next(state);
  t.same(nextState.selected, streamers[1]);
  t.same(nextState.previous, streamers[0]);
  t.same(nextState.next, streamers[2]);
});

test('previous', t => {
  const state = initState();
  const previousState = previous(state);
  t.same(previousState.selected, streamers[2]);
  t.same(previousState.previous, streamers[1]);
  t.same(previousState.next, streamers[0]);
});

test('sibling', t => {
  const state = initState();
  const transform = tabReducer(state, { type: 'GET_SIBLING' });
  t.same(transform.previous, state.previous);
});

test('navigate: do a turn and a move backward', t => {
  const state = initState();
  const fastBackward = backward(backward(backward(backward(state))));
  t.same(fastBackward.selected, R.last(streamers));
});

test('navigate: do a turn and a move forward ', t => {
  const state = initState();
  const fastForward = forward(forward(forward(forward(state))));
  t.same(fastForward.selected, R.head(R.tail(streamers)));
});

test('tabReducer:default', t => {
  const state = initState();
  t.same(tabReducer(state), state);
});

test('tabReducer:ADD_TAB', t => {
  const state = initState();
  const tab = {
    id: 3,
    name: 'john',
    twitch: {
      video: 'john',
      chat: 'john',
    },
    kageshi: 'john',
  };
  const transform = tabReducer(state,
                               { type: 'ADD_TAB', tab });
  t.is(transform.streamers.length,
       state.streamers.length + 1);
});

test('tabReducer:SELECT_TAB', t => {
  const state = initState();
  const transform = tabReducer(state,
                               { type: 'SELECT_TAB', name: 'john' });
  t.same(transform.selected, streamers[2]);
  t.same(transform.previous, streamers[1]);
  t.same(transform.next, streamers[0]);
});

test('tabReducer:PREVIOUS_TAB', t => {
  const state = initState();
  const transform = previous(state);
  t.same(transform.selected,
         tabReducer(state, { type: 'PREVIOUS_TAB' }).selected);
});

test('tabReducer:REMOVE_TAB', t => {
  const state = initState();
  const transform = tabReducer(state, { type: 'REMOVE_TAB', id: 1 });
  t.same(state.streamers.length - 1, transform.streamers.length);
});

test('tabReducer:EDIT_TAB', t => {
  const state = initState();
  const transform = tabReducer(state,
    {
      type: 'EDIT_TAB',
      id: 1,
      payload: { twitch: 'editedTwitch' },
    }
  );
  const updatedStreamer = R.head(
    transform.streamers.filter(streamer => streamer.id === 1));
  t.is(updatedStreamer.twitch, 'editedTwitch');
});
